#include "stdafx.h"
#include "BasicDibMgr.h"

namespace BasicSkinView
{

	CBasicDibMgr::CBasicDibMgr()
	{
		m_hMemBmp = NULL;
		memset(&m_bmpHeader,0,sizeof(m_bmpHeader));
		m_pBits = NULL;
	}

	CBasicDibMgr::~CBasicDibMgr()
	{
		if (m_hMemBmp)
			::DeleteObject(m_hMemBmp);
	}

	bool CBasicDibMgr::Create( HDC hdc,int cx,int cy )
	{
		m_bmpHeader.biSize = sizeof(m_bmpHeader);
		m_bmpHeader.biBitCount = 32;
		m_bmpHeader.biCompression = BI_RGB;
		m_bmpHeader.biWidth = cx;
		m_bmpHeader.biHeight = cy;
		m_bmpHeader.biPlanes = 1;
		m_hMemBmp = CreateDIBSection(hdc, (BITMAPINFO *)&m_bmpHeader, DIB_RGB_COLORS, (void **)&m_pBits, NULL, 0);
		return m_hMemBmp != NULL;
	}

	void CBasicDibMgr::OpenAlpha()
	{
		int nSize = m_bmpHeader.biWidth * m_bmpHeader.biHeight * 4;
		if(nSize <= 0 || nSize % 4)
		{
			return;
		}
		DWORD * pSrc = (DWORD*) m_pBits;

		for(int i = 0; i < nSize; i += 4)
		{
			if(*pSrc & 0xff000000)
			{
				*pSrc = 0;
			}
			else
			{
				*pSrc |= 0xff000000;
			}
			pSrc++;
		}
	}

	void CBasicDibMgr::SetAlpha()
	{
		int nSize = m_bmpHeader.biWidth * m_bmpHeader.biHeight * 4;
		if(nSize <= 0 || nSize % 4)
		{
			return;
		}
		DWORD * pSrc = (DWORD*) m_pBits;

		for(int i = 0; i < nSize; i += 4)
		{
			*pSrc |= 0xff000000;
			pSrc++;
		}
	}
}